//
//  QrCodeViewController.m
//  HQJFacilitator
//
//  Created by mymac on 16/9/26.
//  Copyright © 2016年 Fujian first time iot technology investment co., LTD. All rights reserved.
//

#import "QrCodeViewController.h"
//#import "WXApiRequestHandler.h"
//#import "WXApiManager.h"
#import "WXApi.h"
#import "WXMediaMessage+messageConstruct.h"

//#define HQJTagName @""
//#define HQJTitle @""
//#define HQJDescription @""
static NSString *HQJTagName = @"HQJFACILITATOR_TAG_JUMP_SHOWRANK";
static NSString *HQJTitle = @"会员注册";
static NSString *HQJDescription = @"构建全球生态消费第一管理服务平台";
@interface QrCodeViewController ()<WXApiDelegate>
{
    
    NSString *_shareUrlStr;
}
@property (nonatomic,strong)UIImageView *CodeImageView;
@property (nonatomic,strong)NSArray *titleImageViewArray;
@property (nonatomic,strong)UIButton *friendWXBtn;
@property (nonatomic,strong)UIButton *circleWXBtn;
@end

@implementation QrCodeViewController

-(UIImageView *)CodeImageView {
    if (!_CodeImageView) {
        _CodeImageView = [[UIImageView alloc]init];
        [self.view addSubview:_CodeImageView];
    }
    
    
    return _CodeImageView;
}


- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor groupTableViewBackgroundColor];
    _titleImageViewArray =@[@"wechat",
                            @"pyq"];
    [self create];
}
-(void)create {
    
    self.CodeImageView.sd_layout.leftSpaceToView(self.view,(WIDTH-210)/2).topSpaceToView(self.view,64+70).heightIs(210).widthIs(210);
    
      NSString *urlStr = [NSString stringWithFormat:@"%@getOriQRCode/oid/%@",AppOrg_URL,[FileEngine filePathNameReadName:@"memberid" andstye:fileLoginStyle]];
    [RequestEngine HQJFacilitatorRequestDetailsUrl:urlStr complete:^(NSDictionary *dic) {
        [self.CodeImageView sd_setImageWithURL:[NSURL URLWithString:dic[@"result"][@"imgAddress"]]];
        _shareUrlStr = dic[@"result"][@"strReg"];
        
    } andError:^(NSError *error) {
        
    } ShowHUD:YES];
    
    for (NSInteger i=0; i<2; i++) {
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
        btn.tag = i;
        [btn bk_addEventHandler:^(UIButton * _Nonnull sender) {
        } forControlEvents:UIControlEventTouchUpInside];
        [btn setImage:[UIImage imageNamed:_titleImageViewArray[i]] forState:UIControlStateNormal];
        btn.frame = CGRectMake(i%2*(60+100)+(WIDTH-60*2-100)/2,64+70+70+210, 60, 60);
        [self.view addSubview:btn];
        if (btn.tag == 0) {
            _friendWXBtn = btn;
        } else {
            _circleWXBtn = btn;
        }
    }
    
#pragma mark --
#pragma mark ---好友
    [_friendWXBtn bk_addEventHandler:^(id  _Nonnull sender) {
        if (_shareUrlStr) {
                    [self sendLinkURL:_shareUrlStr TagName:HQJTagName Title:HQJTitle Description:HQJDescription ThumbImage:[UIImage imageNamed:@"shareApp"] InScene:WXSceneSession];

        }
            
    } forControlEvents:UIControlEventTouchUpInside];
    
    
#pragma mark --
#pragma mark --- 朋友圈
    [_circleWXBtn bk_addEventHandler:^(id  _Nonnull sender) {
         [self sendLinkURL:_shareUrlStr TagName:HQJTagName Title:HQJTitle Description:HQJDescription ThumbImage:[UIImage imageNamed:@"shareApp"] InScene:WXSceneTimeline];
    } forControlEvents:UIControlEventTouchUpInside];
    
}

//-(void)WXShare:() {
//    
//    SendMessageToWXReq* req = [[SendMessageToWXReq alloc] init];
//    req.text = @"这是测试发送的内容。";
//    req.bText = YES;
//    req.scene = WXSceneSession;
//    
//    [WXApi sendReq:req];
//    
//    
//}
#pragma mark --
#pragma mark ---微信
-(BOOL)sendLinkURL:(NSString *)urlString
           TagName:(NSString *)tagName
             Title:(NSString *)title
       Description:(NSString *)description
        ThumbImage:(UIImage *)thumbImage
           InScene:(enum WXScene)scene {
    WXWebpageObject *ext = [WXWebpageObject object];
    ext.webpageUrl = urlString;
    
    WXMediaMessage *message = [WXMediaMessage messageWithTitle:title
                                                   Description:description
                                                        Object:ext
                                                    MessageExt:nil
                                                 MessageAction:nil
                                                    ThumbImage:thumbImage
                                                      MediaTag:tagName];
    
    SendMessageToWXReq* req = [self requestWithText:nil
                                                   OrMediaMessage:message
                                                            bText:NO
                                                          InScene:scene];
    return [WXApi sendReq:req];
}
- (SendMessageToWXReq *)requestWithText:(NSString *)text OrMediaMessage:(WXMediaMessage *)message bText:(BOOL)bText InScene:(enum WXScene)scene {
    
    SendMessageToWXReq *req = [[SendMessageToWXReq alloc] init] ;
    req.bText = bText;
    req.scene = scene;
    if (bText)
        req.text = text;
    else
        req.message = message;
    return req;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
